home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Advanced I⁄O v2.3 / Advanced i⁄o / varithm.cc < prev    next >
C/C++ Source or Header  |  1995-06-15  |  6KB  |  233 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. //            Verify the arithmetic coding
  3.  
  4. #include "arithm_modadh.h"
  5. #include "arithm_modadapt.h"
  6.  
  7. static int * MyPattern;
  8. static int MyPattern_size;
  9.  
  10. static void make_pattern()
  11. {
  12.   const int some_pattern [] = {1, 0, 3, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0};
  13.   MyPattern_size = sizeof(some_pattern) / sizeof(some_pattern[0]) + 1000;
  14.   MyPattern = new int[MyPattern_size];
  15.  
  16.   register int i;
  17.   for(i=0; i < (signed)(sizeof(some_pattern) / sizeof(some_pattern[0])); i++)
  18.     MyPattern[i] = some_pattern[i];
  19.   for(; i<MyPattern_size; i++)
  20.     MyPattern[i] = i & 1;
  21. }
  22.  
  23.                 // Test the arithmetic coding with an Adaptive
  24.                 // Histogram model
  25. static void test_adh(void)
  26. {
  27.   message("\nCreating Histogram ...\n");
  28.   Histogram histogram(-7,7);
  29.   register int i;
  30.   for(i=0; i<MyPattern_size; i++)
  31.     histogram.put(MyPattern[i]);
  32.  
  33.   message("\nWriting data ...");
  34.   AdaptiveHistModel model(histogram);
  35.   ArithmCodingOut ac(model);
  36.   ac.open("/tmp/aa");
  37.   for(i=0; i<MyPattern_size; i++)
  38.     ac.put(MyPattern[i]);
  39.   ac.close();
  40.  
  41.   message("\nCoded file /tmp/aa has been created\n");
  42.  
  43.   AdaptiveHistModel i_model;
  44.   ArithmCodingIn ac1(i_model);
  45.   ac1.open("/tmp/aa");
  46.   for(i=0; i<MyPattern_size; i++)
  47.   {
  48.     register int val_read = ac1.get();
  49.     if( i < 5 )
  50.       message("\nValue read %d",val_read);
  51.     if( val_read != MyPattern[i] )
  52.       _error("Read value %d of the %d-th integer is not what it is "
  53.          "supposed to be, %d",
  54.          val_read, i, MyPattern[i]);
  55.   }
  56.   ac1.get();
  57.   assert( ac1.is_eof() );
  58. }
  59.  
  60.                 // Test the arithmetic coding with an Adaptive
  61.                 // model
  62. static void test_adaptive(void)
  63. {
  64.   register int i;
  65.   const short magic_short = 15745;
  66.  
  67.   message("\n\nTesting the arithmetic coding with an AdaptiveModel\n");
  68.  
  69.   {
  70.     message("\nWriting data ...");
  71.     EndianOut o_stream("/tmp/aa");
  72.     o_stream.write_short(magic_short);
  73.  
  74.     AdaptiveModel model(-3,4);
  75.     ArithmCodingOut ac(model);
  76.     ac.open(o_stream);
  77.     ac.set_bigendian();
  78.     for(i=0; i<MyPattern_size; i++)
  79.       ac.put(MyPattern[i]);
  80.     //    ac.close();             inferred upon destruction
  81.     //    o_stream.close();
  82.   }
  83.   
  84.   message("\nCoded file /tmp/aa has been created\n");
  85.  
  86.   EndianIn i_stream("/tmp/aa");
  87.   assert( i_stream.read_short("Reading a magic number") == magic_short );
  88.  
  89.   AdaptiveModel i_model(-3,4);
  90.   ArithmCodingIn ac1(i_model);
  91.   ac1.open(i_stream);
  92.   ac1.set_bigendian();
  93.   for(i=0; i<MyPattern_size; i++)
  94.   {
  95.     register int val_read = ac1.get();
  96.     if( i < 5 )
  97.       message("\nValue read %d",val_read);
  98.     if( val_read != MyPattern[i] )
  99.       _error("Read value %d of the %d-th integer is not what it is "
  100.          "supposed to be, %d",
  101.          val_read, i, MyPattern[i]);
  102.   }
  103. }
  104.  
  105.                 // Another test the arithmetic coding with
  106.                 // an Adaptive model
  107. static void test_adaptive1(void)
  108. {
  109.   register int i;
  110.   const int pattern [] = {0,0,0,0,1,0};
  111.  
  112.   message("\n\nTesting the arithmetic coding with an AdaptiveModel\n");
  113.  
  114.   message("\nWriting data ...");
  115.  
  116.   AdaptiveModel model(-1,4);
  117.   ArithmCodingOut ac(model);
  118.   ac.open("/tmp/aa");
  119.   ac.set_littlendian();
  120.   for(i=0; i<sizeof(pattern)/sizeof(pattern[0]); i++)
  121.     ac.put(pattern[i]);
  122.   ac.close();
  123.  
  124.   message("\nCoded file /tmp/aa has been created\n");
  125.  
  126.   EndianIn i_stream("/tmp/aa");
  127.   AdaptiveModel i_model(-1,4);
  128.   ArithmCodingIn ac1(i_model);
  129.   ac1.open(i_stream);
  130.   ac1.set_littlendian();
  131.   for(i=0; i<sizeof(pattern)/sizeof(pattern[0]); i++)
  132.   {
  133.     register int val_read = ac1.get();
  134.     if( i < 5 )
  135.       message("\nValue read %d",val_read);
  136.     if( val_read != pattern[i] )
  137.       _error("Read value %d of the %d-th integer is not what it is "
  138.          "supposed to be, %d",
  139.          val_read, i, pattern[i]);
  140.   }
  141.   ac1.get();
  142.   assert( ac1.is_eof() );
  143. }
  144.  
  145.                 // Test two encoded streams, one after another
  146. static void test_two_streams(void)
  147. {
  148.   register int i;
  149.   const int pattern1 [] = {0,0,0,0,0,0,1,0,4,4};
  150.   const int pattern2 [] = {1,0,0,1,0,1};
  151.  
  152.   message("\n\nTesting the concatenating of two streams\n");
  153.  
  154.   message("\nWriting data ...");
  155.  
  156.   EndianOut stream("/tmp/aa");
  157.   stream.set_littlendian();
  158.   const int ethalon = 12345;
  159.  
  160.   {
  161.     AdaptiveModel model(-1,4);
  162.     ArithmCodingOut ac(model);
  163.     ac.open(stream);
  164.     for(i=0; i<sizeof(pattern1)/sizeof(pattern1[0]); i++)
  165.       ac.put(pattern1[i]);
  166.   }
  167.  
  168.   {
  169. #ifndef __MWERKS__
  170.     message("\nPosition 0%o\n",stream.tellp());
  171. #endif
  172.     stream.write_long(ethalon);
  173.     AdaptiveModel model(-1,4);
  174.     ArithmCodingOut ac(model);
  175.     ac.open(stream);
  176.     for(i=0; i<sizeof(pattern2)/sizeof(pattern2[0]); i++)
  177.       ac.put(pattern2[i]);
  178.   }
  179.  
  180.   stream.close();
  181.  
  182.   message("\nCoded file /tmp/aa has been created\n");
  183.  
  184.   EndianIn i_stream("/tmp/aa");
  185.   i_stream.set_littlendian();
  186.  
  187.   {
  188.     AdaptiveModel i_model(-1,4);
  189.     ArithmCodingIn ac1(i_model);
  190.     ac1.open(i_stream);
  191.     for(i=0; i<sizeof(pattern1)/sizeof(pattern1[0]); i++)
  192.     {
  193.       register int val_read = ac1.get();
  194.       if( i < 5 )
  195.     message("\nValue read %d",val_read);
  196.       if( val_read != pattern1[i] )
  197.     _error("Read value %d of the %d-th integer is not what it is "
  198.            "supposed to be, %d",
  199.            val_read, i, pattern1[i]);
  200.     }
  201.     ac1.get();
  202.     assert( ac1.is_eof() );
  203.   }
  204.  
  205.   {
  206.     message("\nPosition 0%o\n",i_stream.tellg());
  207.     assert( i_stream.read_long("Reading Ethalon") == ethalon );
  208.     AdaptiveModel i_model(-1,4);
  209.     ArithmCodingIn ac1(i_model);
  210.     ac1.open(i_stream);
  211.     for(i=0; i<sizeof(pattern2)/sizeof(pattern2[0]); i++)
  212.     {
  213.       register int val_read = ac1.get();
  214.       if( i < 5 )
  215.     message("\nValue read %d",val_read);
  216.       if( val_read != pattern2[i] )
  217.     _error("Read value %d of the %d-th integer is not what it is "
  218.            "supposed to be, %d",
  219.            val_read, i, pattern2[i]);
  220.     }
  221.   }
  222.   message("\nDone\n");
  223. }
  224.  
  225. main(void)
  226. {
  227.   make_pattern();
  228.   test_adh();
  229.   test_adaptive();
  230.   test_adaptive1();
  231.   test_two_streams();
  232. }
  233.